<?php
declare (strict_types=1);

namespace app\common\tool;

class Sms
{
    public static function send($mobile, $code)
    {
        $url = 'https://api.mix2.zthysms.com/v2/sendSms';
        try {
            $config = config('sms');
            $tkey = time();
            $data['json'] = [
                'tKey' => $tkey,
                'mobile' => $mobile,
                'content' => $config['sms_sign'] . '您的验证码是【' . $code . '】，验证码5分钟内有效',
                'username' => $config['sms_username'],
                'password' => md5(md5($config['sms_password']) . $tkey),
                'action' => 'send'
            ];
            $result = ApiClient::create($url)->post('', $data);
            if ($result->isSuccess()) {
                $result = $result->toArray();
                if ($result['code'] != 200) {
                    throw new \Exception($result['msg']);
                }
            } else {
                throw new \Exception('请求失败');
            }

        } catch (\Exception $e) {

            return $e->getMessage();
        }

        return true;
    }
}